import com.sun.deploy.net.HttpUtils;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 东莞呵呵
 * Date:2022-10-01
 * Time:11:14
 */
public class HttpClient {
    private String ip;
    private int port;
    private Socket socket;

    public HttpClient(String ip, int port) throws IOException {
        this.ip = ip;
        this.port = port;
        this.socket = new Socket(ip, port);
    }

    public String get(String url) throws IOException {
        StringBuilder request = new StringBuilder();
        request.append("GET " + url + "HTTP/1.1\n");
        request.append("Host: " + this.ip + ":" + this.port + "\n");
        request.append("\n");

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(request.toString().getBytes());

        InputStream inputStream = socket.getInputStream();

        byte[] buffer = new byte[2 * 1024 * 1024];
        int n = inputStream.read(buffer);
        return new String(buffer, 0, n, "utf-8");
    }

    public String post(String url, String body) throws IOException {
        StringBuilder request = new StringBuilder();

        request.append("POST " + url + " HTTP/1.1\n");
        request.append("Host: " + ip + ":" + port + "\n");
        request.append("Content-Length: " + body.getBytes().length + "\n");
        request.append("Content-Type: text/plain\n");
        request.append("\n");
        request.append(body);

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(request.toString().getBytes());

        InputStream inputStream = socket.getInputStream();

        byte[] buffer = new byte[2 * 1024 * 1024];
        int n = inputStream.read(buffer);
        return new String(buffer, 0, n, "utf-8");
    }

    public static void main(String[] args) {
        HttpUtils
    }
}
